package xyz.xymu.oreo.testwindowmanager

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.Service
import android.content.Intent
import android.os.Build
import android.os.IBinder
import android.util.Log
import androidx.annotation.RequiresApi

class LandService : Service() {

    override fun onCreate() {
        super.onCreate()



        // 开启前台服务
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createForegroundService()
        }
    }

    override fun onBind(intent: Intent): IBinder? {
        return null
    }

    @RequiresApi(Build.VERSION_CODES.O)
    private fun createForegroundService() {
        // 创建渠道
        val channelId = "PerService"
        val name = getString(R.string.app_name)
        val descriptionText = getString(R.string.app_name)
        val importance = NotificationManager.IMPORTANCE_LOW
        val channel = NotificationChannel(channelId, name, importance).apply {
            description = descriptionText
        }
        val notificationManager: NotificationManager =
            getSystemService(Service.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.createNotificationChannel(channel)

        // 创建通知
        val notification: Notification = Notification.Builder(this, channelId)
            .setContentTitle(getText(R.string.app_name))
            .setContentText(getText(R.string.app_name))
            .build()

        // 启动前台通知
        startForeground(186, notification)
        Log.i("宠物服务", "前台服务 创建完成")
    }
}